package com.healthdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.healthdata.dao.BedUtilizationMapper;
import com.healthdata.entity.BedUtilization;
import com.healthdata.service.BedUtilizationService;
import com.healthdata.vo.BedUtilizationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class BedUtilizationServiceImpl implements BedUtilizationService {

    @Autowired
    private BedUtilizationMapper bedUtilizationMapper;

    @Override
    public List<BedUtilizationVO> listAll() {
        List<BedUtilization> entityList = bedUtilizationMapper.selectList(null);
        return convertToVOList(entityList);
    }

    @Override
    public BedUtilizationVO getById(Integer id) {
        BedUtilization entity = bedUtilizationMapper.selectById(id);
        return entity != null ? convertToVO(entity) : null;
    }

    @Override
    public List<BedUtilizationVO> listByInstitutionType(String institutionType) {
        QueryWrapper<BedUtilization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("institution_type", institutionType);
        return convertToVOList(bedUtilizationMapper.selectList(queryWrapper));
    }

    @Override
    public List<BedUtilizationVO> listByYear(Integer year) {
        QueryWrapper<BedUtilization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        return convertToVOList(bedUtilizationMapper.selectList(queryWrapper));
    }

    // 实体转VO（核心转换逻辑）
    private BedUtilizationVO convertToVO(BedUtilization entity) {
        BedUtilizationVO vo = new BedUtilizationVO();
        BeanUtils.copyProperties(entity, vo);

        // 数值格式化
        vo.setTotalOpenBedDaysStr(formatNumber(entity.getTotalOpenBedDays()));
        vo.setBedTurnoverRateStr(formatPercentage(entity.getBedTurnoverRate()));
        vo.setBedWorkingDaysStr(formatDecimal(entity.getBedWorkingDays()));
        vo.setBedUtilizationRateStr(formatPercentage(entity.getBedUtilizationRate()));
        vo.setAvgHospitalStayStr(formatDecimal(entity.getAvgHospitalStay()));

        // 计算床位紧张程度
        if (entity.getBedUtilizationRate() != null) {
            double rate = entity.getBedUtilizationRate().doubleValue();
            System.out.println(rate);
            if (rate > 80) {
                vo.setBedTensionLevel("紧张");
            } else if (rate > 60) {
                vo.setBedTensionLevel("适中");
            } else {
                vo.setBedTensionLevel("宽松");
            }
        } else {
            vo.setBedTensionLevel("未知");
        }

        return vo;
    }

    private List<BedUtilizationVO> convertToVOList(List<BedUtilization> entityList) {
        List<BedUtilizationVO> voList = new ArrayList<>();
        for (BedUtilization entity : entityList) {
            voList.add(convertToVO(entity));
        }
        return voList;
    }

    // 格式化工具方法
    private String formatNumber(Integer number) {
        return number == null ? "0" : String.valueOf(number);
    }

    private String formatDecimal(BigDecimal decimal) {
        return decimal == null ? "0" : decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    private String formatPercentage(BigDecimal decimal) {
        return decimal == null ? "0%"
                : decimal.multiply(new BigDecimal("100"))
                .setScale(2, BigDecimal.ROUND_HALF_UP)
                .toString() + "%";
    }
}